package com.xiaolong.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(urlPatterns="/validateImgServlet")
public class ValidateImgServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int width = 318;
    	int height = 200;
    	//create the image
    	BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    	Graphics g = image.getGraphics();
    	// set the background color
    	g.setColor(new Color(0xDCDCDC));
    	g.fillRect(0, 0, width, height);
    	// draw the border
    	g.setColor(Color.gray);
    	g.drawRect(0, 0, width - 1, height - 1);
    	// create a random instance to generate the codes
    	Random rdm = new Random();
    	String hash1 = Integer.toHexString(rdm.nextInt());
    	System.out.print(hash1);
    	// make some confusion
    	for (int i = 0; i < 50; i++) {
    		int x = rdm.nextInt(width);
    		int y = rdm.nextInt(height);
    		g.drawOval(x, y, 0, 0);
    	}
    	// generate a random code
    	String capstr = hash1.substring(0, 4);
    	HttpSession session = req.getSession(true);
    	//将生成的验证码存入session
    	session.setAttribute("validateImg", capstr);
    	g.setColor(new Color(0, 100, 0));
    	g.setFont(new Font("Candara", Font.BOLD, 24));
    	g.drawString(capstr, 8, 24);
    	
		g.drawImage(this.createBufferedImage(75, 88, BufferedImage.TYPE_INT_RGB), 5,8, null);
		g.drawImage(this.createBufferedImage(75, 88, BufferedImage.TYPE_INT_RGB), 83,8, null);
		g.drawImage(this.createBufferedImage(75, 88, BufferedImage.TYPE_INT_RGB), 161,8, null);
		g.drawImage(this.createBufferedImage(75, 88, BufferedImage.TYPE_INT_RGB), 238,8, null);
		g.drawImage(this.createBufferedImage(75, 88, BufferedImage.TYPE_INT_RGB), 5,104, null);
		g.drawImage(this.createBufferedImage(75, 88, BufferedImage.TYPE_INT_RGB), 83,104, null);
		g.drawImage(this.createBufferedImage(75, 88, BufferedImage.TYPE_INT_RGB), 161,104, null);
		g.drawImage(this.createBufferedImage(75, 88, BufferedImage.TYPE_INT_RGB), 238,104, null);
    	g.dispose();
    	//输出图片
    	resp.setContentType("image/jpeg");
    	OutputStream strm = resp.getOutputStream();
    	ImageIO.write(image, "jpeg", strm);
    	strm.close(); 
	}
	
	public BufferedImage createBufferedImage(int width, int height, int imageType) throws IOException{
		Random r = new Random();
		int i = r.nextInt(12);
		i+=1;
		String url = "";
		if(i<10){
			url = "static/img/user/0"+i+".jpg";
		}else{
			url = "static/img/user/"+i+".jpg";
		}
		InputStream imagein = ClassLoader.getSystemResourceAsStream(url);//new FileInputStream("resources/static/img/user/01.jgp");
		BufferedImage image = ImageIO.read(imagein);//new BufferedImage(width, height, imageType);
		BufferedImage tag = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);     
        Graphics2D g = tag.createGraphics();
		//Graphics g = passImg.getGraphics();
		//g.setColor(new Color(0xDCDCDC));
    	//g.fillRect(0, 0, width, height);
    	g.drawImage(image.getScaledInstance(width, height,  Image.SCALE_SMOOTH), 0, 0, null);
    	// draw the border
    	//g.setColor(Color.gray);
    	//g.drawRect(0, 0, width - 1, height - 1);
    	//g.setColor(new Color(0, 100, 0));
    	//g.setFont(new Font("Candara", Font.BOLD, 30));
    	//g.drawString("ABC", 8, 24);
		return tag;
	}
}
